﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace Framework.Infrastructure
{
    public static class MSContext42Utility
    {
        public static IQueryable<TElement> GetQueryable<TEntity, TElement>(this TEntity entity, string collectionName)
            where TEntity : DbEntity
            where TElement : class
        {
            IQueryable<TElement> query = null;
            if (entity.EntityEntry != null)
            {
                query = entity.EntityEntry.Collection(collectionName).Query().Cast<TElement>();
            }
            return query;
        }

        public static IQueryable<TElement> GetQueryable<TEntity, TElement>(this TEntity entity, Expression<Func<TEntity, ICollection<TElement>>> navigationProperty)
            where TEntity : DbEntity
            where TElement : class
        {
            IQueryable<TElement> query = null;
            query = entity.EntityEntry.Cast<TEntity>().Collection(navigationProperty).Query().Cast<TElement>();
            return query;
        }

        public static void ReferenceLoad<TEntity, TProperty>(this TEntity entity, Expression<Func<TEntity, TProperty>> navigationProperty)
            where TEntity : DbEntity
            where TProperty : class
        {
            entity.EntityEntry.Cast<TEntity>().Reference(navigationProperty).Load();
        }

        public static void CollectionLoad<TEntity, TElement>(this TEntity entity, Expression<Func<TEntity, ICollection<TElement>>> navigationProperty)
            where TEntity : DbEntity
            where TElement : class
        {
            entity.EntityEntry.Cast<TEntity>().Collection(navigationProperty).Load();
        }
    }
}
